package controller;

import net.oschina.j2cache.Cache;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cache")
public class UserController {
    private String key="myKey";
    private String region="default";
    @Autowired
    private CacheChannel cacheChannel;
    @GetMapping("/getInfos")
    public List<String> getInfos(){
        CacheObject cacheObject=cacheChannel.get(region,key);
        if(cacheObject.getValue()==null){
            List<String> data=new ArrayList<>();
            data.add("info1");
            data.add("info2");
            cacheChannel.set(region,key,data);
            return data;
        }
        return (List<String>) cacheObject.getValue();
    }

}
